﻿using DLL;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.NutritionalRisk
{
    public partial class AddNutritionalRisk : Form
    {
        #region 变量声明
        public NursingFormSystem.NurFormsService.NursingForm formService = new NursingFormSystem.NurFormsService.NursingForm();
        NursingFormRecord record = new NursingFormRecord();
        Tb_User LoginUser;
        string Id = string.Empty;
        PatientInfo Patientinfo;
        int Sum = 0;
        #endregion

        public AddNutritionalRisk(Tb_User loginUser, PatientInfo patientInfo, string id)
        {
            InitializeComponent();
            //成员变量赋值
            this.Patientinfo = patientInfo;
            this.LoginUser = loginUser;
            this.Id = id;
            //绑定数据
            BindInfo(Id);
        }
        public AddNutritionalRisk(string[] loginUser, string[] patientInfo)
        {
            InitializeComponent();
            //成员变量赋值
            this.Patientinfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Id = string.Empty;
            //绑定数据
            BindInfo(this.Id);
        }
        public AddNutritionalRisk(string[] loginUser, string[] patientInfo, string id)
        {
            InitializeComponent();
            //成员变量赋值
            this.Patientinfo = ArrayToObject.ArrayToPatientInfo(patientInfo);
            this.LoginUser = ArrayToObject.ArrayToLoginUser(loginUser);
            this.Id = id;
            //绑定数据
            BindInfo(Id);
        }

        #region 显示赋值
        public void BindInfo(string Id)
        {
            textHuShiQianMing.Text = LoginUser.UserName;   //护士签名
            dateEditPingGuRiQi.DateTime = DateTime.Now;   //默认显示当前日期
            textZongFen.Text = "0";
            if (string.IsNullOrEmpty(Id)) return;
            string sql = string.Format("SELECT TOP 1 NursingFormData FROM NursingFormRecord(NOLOCK) WHERE Id='{0}'", Id);
            DataTable tab = SqlHelper.ExecuteDataset(sql, System.Data.CommandType.Text).Tables[0];
            var NursingFormData = Convert.ToString(tab.Rows[0]["NursingFormData"]);
            Nutritions nss = new Nutritions();
            nss = XmlUtil.XMLToObject<Nutritions>(NursingFormData, Encoding.UTF8);
            if (!string.IsNullOrEmpty(nss.PingGuRiQi))
                dateEditPingGuRiQi.DateTime = Convert.ToDateTime(nss.PingGuRiQi);   //评估日期
            textZongFen.Text = nss.ZongFen.ToString();   //总分
            textTiZhong.Text = nss.JiChuZiLiao_TiZhong;  //体重
            textShenGao.Text = nss.ShenGao;   //身高
            textTiZhongZhiShu.Text = nss.JiChuZiLiao_TiZhongZhiShu;   //体重指数
            textDanBaiZhi.Text = nss.JiChuZiLiao_DanBaiZhi;   //蛋白质
            textHuShiQianMing.Text = nss.HuShiQianMing;    //护士签名
            #region 疾病状态
            string[] JiBingZhuangT = nss.JiBingZhuangTai.Split('#');
            if (JiBingZhuangT[0] == "1")
            {
                checkJiBingZhuangTai1.Checked = true;
            }
            else
            {
                checkJiBingZhuangTai1.Checked = false;
            }
            if (JiBingZhuangT[1] == "1")
            {
                checkJiBingZhuangTai2.Checked = true;
            }
            else
            {
                checkJiBingZhuangTai2.Checked = false;
            }
            if (JiBingZhuangT[2] == "1")
            {
                checkJiBingZhuangTai3.Checked = true;
            }
            else
            {
                checkJiBingZhuangTai3.Checked = false;
            }
            #endregion

            #region 营养状态
            string[] YingYangZhuangT = nss.YingYangZhuangTai.Split('#');
            if (YingYangZhuangT[0] == "1")
            {
                checkYingYangZhuangTai1.Checked = true;
            }
            else
            {
                checkYingYangZhuangTai1.Checked = false;
            }
            if (YingYangZhuangT[1] == "1")
            {
                checkYingYangZhuangTai2.Checked = true;
            }
            else
            {
                checkYingYangZhuangTai2.Checked = false;
            }
            if (YingYangZhuangT[2] == "1")
            {
                checkYingYangZhuangTai3.Checked = true;
            }
            else
            {
                checkYingYangZhuangTai3.Checked = false;
            }
            if (YingYangZhuangT[3] == "1")
            {
                checkYingYangZhuangTai4.Checked = true;
            }
            else
            {
                checkYingYangZhuangTai4.Checked = false;
            }
            #endregion

            #region 年龄
            if (nss.NianLing == "0")
                checkNianLing.Checked = true;
            else
                checkNianLing.Checked = false;
            #endregion

            #region 处理
            string[] ChuL = nss.ChuLi.Split('#');
            if (ChuL[0] == "1")
            {
                checkChuLi1.Checked = true;
            }
            else
            {
                checkChuLi1.Checked = false;
            }
            if (ChuL[1] == "1")
            {
                checkChuLi2.Checked = true;
            }
            else
            {
                checkChuLi2.Checked = false;
            }
            #endregion

        }
        #endregion

        #region 保存
        private void simpleButtonbaocun_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(dateEditPingGuRiQi.Text.Trim()))
                dateEditPingGuRiQi.DateTime = DateTime.Now;

            Nutritions ns = new Nutritions();
            ns.PingGuRiQi = dateEditPingGuRiQi.DateTime.ToString("yyyy-MM-dd HH:mm:ss"); //评估日期
            ns.ZongFen = textZongFen.Text;  //总分
            ns.ShenGao = textShenGao.Text;   //身高
            ns.JiChuZiLiao_TiZhong = textTiZhong.Text;  //体重
            ns.JiChuZiLiao_TiZhongZhiShu = textTiZhongZhiShu.Text;  //体重指数
            ns.JiChuZiLiao_DanBaiZhi = textDanBaiZhi.Text;  //蛋白质
            ns.JiBingZhuangTai = (checkJiBingZhuangTai1.Checked ? "1" : "0") + "#" + (checkJiBingZhuangTai2.Checked ? "1" : "0") + "#" +
                                 (checkJiBingZhuangTai3.Checked ? "1" : "0");  //疾病状态
            ns.YingYangZhuangTai = (checkYingYangZhuangTai1.Checked ? "1" : "0") + "#" + (checkYingYangZhuangTai2.Checked ? "1" : "0") + "#" +
                                   (checkYingYangZhuangTai3.Checked ? "1" : "0") + "#" + (checkYingYangZhuangTai4.Checked ? "1" : "0");  //营养状态
            ns.NianLing = checkNianLing.Checked ? "0" : "-1";   //年龄
            ns.ChuLi = (checkChuLi1.Checked ? "1" : "0") + "#" + (checkChuLi2.Checked ? "1" : "0");   //处理
            //护士签名
            if (string.IsNullOrEmpty(this.Id))
                ns.HuShiQianMing = this.LoginUser.UserName;
            else
                ns.HuShiQianMing = textHuShiQianMing.Text;

            ns.FormId = (int)FormType.Pabulum;   //表单ID
            ns.FormName = EnumHelper.GetDescription(FormType.Pabulum);   //表单名字
            record.FormId = (int)FormType.Pabulum;   //表单ID
            record.FormName = EnumHelper.GetDescription(FormType.Pabulum);   //表单名字
            record.PatientId = Patientinfo.PATIENT_ID;   //住院号
            record.VisitId = Patientinfo.VISIT_ID;    //住院次数
            record.PatientName = Patientinfo.PATIENT_NAME;  //患者姓名
            record.BedNo = Patientinfo.BED_NO;   //床号
            record.DepartmentId = Patientinfo.DEPARTMENT_ID;   //当前患者科室ID
            record.DepartmentName = Patientinfo.DEPARTMENT_NAME;   //当前患者科室名称
            record.AreaId = Patientinfo.NURSE_CELL_CODE;   //当前患者病区ID
            record.AreaName = Patientinfo.NURSE_CELL_NAME;   //当前患者病区名称
            record.AssessmentDate = dateEditPingGuRiQi.DateTime;   //时间
            record.Id = Id == "" ? 0 : Convert.ToInt32(Id);
            record.NursingFormData = XmlUtil.ObjectToXML(ns, Encoding.UTF8);
            var ret = formService.SetNursingFormRecord(Newtonsoft.Json.JsonConvert.SerializeObject(record));
            if (ret.Code == 1)
            {
                MessageBox.Show(ret.M);
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                MessageBox.Show(ret.M);
            }
        }
        #endregion

        #region 计算总分
        public void SunTotal()
        {
            int Total = 0;
            int Total1 = 0;
            int Total2 = 0;

            //疾病状态
            if (checkJiBingZhuangTai1.Checked)
                Total += 1;
            else
                Total += 0;

            if (checkJiBingZhuangTai2.Checked)
                Total += 2;
            else
                Total += 0;

            if (checkJiBingZhuangTai3.Checked)
                Total += 3;
            else
                Total += 0;

            //营养状态
            if (checkYingYangZhuangTai1.Checked)
                Total1 += 0;
            else
                Total1 += 0;

            if (checkYingYangZhuangTai2.Checked)
                Total1 += 1;
            else
                Total1 += 0;

            if (checkYingYangZhuangTai3.Checked)
                Total1 += 2;
            else
                Total1 += 0;
            if (checkYingYangZhuangTai4.Checked)
                Total1 += 3;
            else
                Total1 += 0;

            //年龄
            if (checkNianLing.Checked)
                Total2 += 1;
            else
                Total2 += 0;

            Sum = Total + Total1 + Total2;
            textZongFen.Text = Sum.ToString();

        }
        #endregion

        #region 取消
        private void simpleButtonquxiao_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }
        #endregion

        #region 设置 enter键当做Tab切换
        /// <summary>
        /// 设置 enter键当做Tab切换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void TemperatureEdit1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }
        #endregion

        #region 总分
        private void checkJiBingZhuangTai1_CheckedChanged(object sender, EventArgs e)
        {
            SunTotal();
        }
        #endregion
    }
}